// Copyright 叶湘 ( weixin:yexiang841, email:yexiang841@qq.com ), Shitouren Studio. All Rights Reserved.

#include "Hit/Q3D_CPPC_HitCauser.h"
#include "Hit/Q3D_CPPU_Hit.h"
#include "Logger/Q3D_CPPU_Logger.h"

// Sets default values for this component's properties
UQ3D_CPPC_HitCauser::UQ3D_CPPC_HitCauser()
{
	// Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
	// off to improve performance if you don't need them.
	PrimaryComponentTick.bCanEverTick = true;

	// ...
}

// Called when the game starts
void UQ3D_CPPC_HitCauser::BeginPlay()
{
	Super::BeginPlay();

	// ...
}

void UQ3D_CPPC_HitCauser::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
	Super::EndPlay(EndPlayReason);
	/* 还没到决定要不要这么做的时候
	TArray<UQ3D_CPPU_Hit*> array_values;
	this->Map_Name_To_Ptr_U_Hit.GenerateValueArray( array_values );
	for( UQ3D_CPPU_Hit* one : array_values ){
		one->Q3D_ED_Hit_Done.RemoveAll( this );
		one->Q3D_ED_Hit_Cancel.RemoveAll( this );
		one->Q3D_ED_Halo_Cancel.RemoveAll( this );
		one->Q3D_ED_Hit_Done.RemoveAll( this );
	}
	int32 int_count;
	this->Q3D_F_Hit_Cancel_All( int_count );
	*/
}

// Called every frame
void UQ3D_CPPC_HitCauser::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

	// ...
}

void UQ3D_CPPC_HitCauser::Q3D_F_Hit_Set_Implementation(const TMap<FName, UQ3D_CPPU_Hit *> &map_name_to_ptr_u_hit_param, bool &bool_success, FString &string_log)
{
	int32 int_count_tmp = 0;
	bool_success = false;
	string_log = "Set_None";
	TArray<UQ3D_CPPU_Hit *> array_values;
	map_name_to_ptr_u_hit_param.GenerateValueArray(array_values);
	for (UQ3D_CPPU_Hit *one : array_values)
	{
		// 关键步骤：将 Hit 的 Causer 设置为组件的 Owner
		one->Q3D_F_HitCauser_Set(this->GetOwner(), one);
		if (this->Map_Name_To_Ptr_U_Hit.Find(one->Name_Id))
		{
			// 如果 Map 中有重复的 Hit 则跳过
			Q3D_LOG(Warning, "Hit_Duplicate_[" + one->Name_Id.ToString() + "]");
			continue;
		}
		// Q3D_LOG( Verbose, StaticEnum<EQ3D_CPPE_HitMode>()->GetNameStringByValue( (uint8)(one->Q3D_F_HitMode_GetEnum()) ) \
		// 				+ "(" + StaticEnum<EQ3D_CPPE_HitType>()->GetNameStringByValue( (uint8)(one->Q3D_F_HitType_GetEnum()) ) \
		// 				+ ")_On_[" + StaticEnum<EQ3D_CPPE_PropertyName>()->GetNameStringByValue( (uint8)(one->Enum_PropertyName) ) \
		// 				+ "]_Set_[" + one->Name_Id.ToString() + "]" );
		// 将 Hit 放入 Map 暂存
		this->Map_Name_To_Ptr_U_Hit.Add(one->Name_Id, one);
		bool_success = true;
		int_count_tmp += 1;
		string_log = "Hit_Set_Count_[" + FString::FromInt(int_count_tmp) + "]";
	}
	return;
}

void UQ3D_CPPC_HitCauser::Q3D_F_Hit_SetToTarget_Implementation(AActor *ptr_a_hit_target, bool &bool_success, FString &string_log)
{
	int int_hit_count = 0;
	bool_success = false;
	string_log = "";
	TArray<UQ3D_CPPU_Hit *> array_values;
	this->Map_Name_To_Ptr_U_Hit.GenerateValueArray(array_values);
	for (UQ3D_CPPU_Hit *one : array_values)
	{
		if (this->Bool_Ignore_Maker && one->Ptr_A_HitMaker)
		{
			// 跳过条件：
			// 1、设置为不命中 Maker；
			// 2、当前命中携带了 Maker 信息；
			if (ptr_a_hit_target == one->Ptr_A_HitMaker)
			{
				Q3D_LOG(Verbose, "Hit_Ignore_Maker");
				continue;
			}
		}
		// 关键步骤：将 Hit 发往 Target
		one->Q3D_F_SetToTarget(ptr_a_hit_target, bool_success, string_log);
		// Q3D_LOG( Verbose, StaticEnum<EQ3D_CPPE_HitMode>()->GetNameStringByValue( (uint8)(one->Q3D_F_HitMode_GetEnum()) ) \
		// 				+ "(" + StaticEnum<EQ3D_CPPE_HitType>()->GetNameStringByValue( (uint8)(one->Q3D_F_HitType_GetEnum()) ) \
		// 				+ ")_On_[" + StaticEnum<EQ3D_CPPE_PropertyName>()->GetNameStringByValue( (uint8)(one->Enum_PropertyName) ) \
		// 				+ "]_Set_[" + one->Name_Id.ToString() + "]" );
		if (bool_success)
		{
			++int_hit_count;
		}
		else
		{
			Q3D_LOG(Warning, string_log);
		}
	}
	if (int_hit_count > 0)
	{
		bool_success = true;
		string_log = FString::FromInt(int_hit_count) + "Hit_Seted";
	}
	else
	{
		bool_success = false;
		string_log = "No_Hit_Seted";
	}
	return;
}